/**
 * 
 */
package furniture.render3d;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;

import org.eclipse.swt.graphics.RGB;

/**
 * @author Grill Balazs (balage.g@gmail.com)
 *
 */
public class PointSet implements Polygon3d {

	private final List<Point3d> points = new ArrayList<Point3d>();
	
	public RGB color = new RGB(100, 100, 100);
	
	public RGB paint = new RGB(100, 100, 100);
	
	public boolean fill = false;
	
	public PointSet() {
	}
	
	public PointSet(Point3d...points) {
		for(Point3d p : points){
			this.points.add(p);
		}
	}
	
	/**
	 * @see furniture.render3d.Polygon3d#center()
	 */
	public Point3d center() {
		Point3d result = new Point3d(0,0,0);
		for(Point3d p : points)
			result.add(p);
		if (points.size()!=0)
			result.scale(1.0/points.size());
		
		return result;
	}

	/**
	 * @see furniture.render3d.Polygon3d#copy()
	 */
	public Polygon3d copy() {
		PointSet result = new PointSet();
		for(Point3d p : points)
			result.addPoint(new Point3d(p));
		return result;
	}

	/**
	 * @see furniture.render3d.Polygon3d#fill()
	 */
	public boolean fill() {
		return fill;
	}

	/**
	 * @see furniture.render3d.Polygon3d#getPaint()
	 */
	public RGB getPaint() {
		return paint;
	}

	/**
	 * @see furniture.render3d.Polygon3d#getPoints()
	 */
	public List<Point3d> getPoints() {
		return points;
	}

	/**
	 * @see furniture.render3d.Polygon3d#setPoints(java.util.List)
	 */
	public void setPoints(List<Point3d> points) {
		this.points.clear();
		this.points.addAll(points);

	}

	/* *
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	public int compareTo(Polygon3d o) {
		Double a = new Vector3d(this.center()).length();
		Double b = new Vector3d(o.center()).length();
		return a.compareTo(b);
	}

	public void addPoint(Point3d p){
		points.add(p);
	}
	
	/**
	 * Translate all points by the given vector
	 * @param vector
	 */
	public void translate(Vector3d vector){
		for(Point3d p : points){
			p.add(vector);
		}
	}

	public RGB getColor() {
		return color;
	}

	public Collection<Polygon3d> getTriangles() {
		List<Polygon3d> l = new ArrayList<Polygon3d>();
		l.add(this);
		return l;
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		boolean first = true;
		for (Point3d p : points){
			if (first)
				first = false;
			else sb.append(" -> ");
			sb.append(p.toString());
		}
		
		return sb.toString();
	}
	
}
